<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * User Model
 * 
 * @author Ali
 **/
class User_model extends MY_Model {

	public function __construct()
	{
        parent::__construct();
    }

	public function add($data = array())
	{
        $this->db->insert('user', $data);

        return $this->db->insert_id();
    }

	public function simple_get($id, $select = '*')
	{
        $q = $this->db->select($select)->get_where('user', array('id' => $id));

        if (!$q->num_rows())
            return false;

        return $q->row();
    }

	public function get($options = array())
	{
        $this->db
            ->select('t1.*, 
        	t2.firstname, 
        	t2.lastname, 
        	t2.telephone, 
        	t2.company, 
        	t2.birthday, 
        	t2.gender, 
        	t2.address, 
        	t2.city, 
        	t2.town, 
        	t3.name AS role')
            ->from('user AS t1') // table 1
            ->join('user_profile AS t2', 't1.id = t2.user_id', 'left') // table 2
            ->join('user_role AS t3', 't1.role_id = t3.id', 'left'); // table 3
            
        // WHERE
        // By id
        if (isset($options['id']))
            $this->db->where('t1.id', $options['id']);

        // By email
        if (isset($options['email']))
            $this->db->where('t1.email', $options['email']);

        $query = $this->db->get();

        if (!$query->num_rows())
            return false;

        return $query->row();
    }

	public function gets($options = array(), $limit = 0, $offset = 0)
	{
        $this->db
        	->select('SQL_CALC_FOUND_ROWS t1.*, 
		    	t2.firstname, 
		    	t2.lastname, 
		    	t2.telephone, 
		    	t2.company, 
		    	t2.birthday, 
		    	t2.gender, 
		    	t2.address, 
		    	t2.city, 
		    	t2.town, 
		        t3.name AS role', FALSE)
        	->from('user AS t1') // table 1
        	->join('user_profile AS t2', 't1.id = t2.user_id', 'left') // table 2
        	->join('user_role AS t3', 't1.role_id = t3.id', 'left'); // table 3
        	
     	// WHERE
		
		// By role id
        if (isset($options['role_id']) AND $options['role_id'] != 'all')
            $this->db->where('t1.role_id', $options['role_id']);
		
		// LIMIT
        if ($limit > 0) 
            $this->db->limit($limit, $offset);
		
		return parent::my_result();
    }
	
	public function search($needle, $limit = 0, $offset = 0)
	{
        $this->db
            ->select('
                SQL_CALC_FOUND_ROWS 
                u.*, 
                up.firstname, 
                up.lastname, 
                up.telephone, 
                up.company, 
                up.birthday, 
                up.gender, 
                up.address, 
                up.city, 
                up.town,
                r.name AS role
                ', FALSE)
            ->from('user AS u') // table 1
            ->join('user_profile AS up', 'u.id = up.user_id', 'left') // table 2
            ->join('user_role AS r', 'u.role_id = r.id', 'left') // table 3
            ->or_like(array(
                'u.username' => $needle,
                'u.email' => $needle,
                'up.firstname' => $needle,
                'up.lastname' => $needle
            ));

        // WHERE
        // LIMIT
        if ($limit > 0)
            $this->db->limit($limit, $offset);
        
		
		return parent::my_result();
    }

	public function edit($data = array(), $options = array())
	{
        return $this->db->update('user', $data, $options);
    }

	public function delete($id)
	{
        // delete user table
        $this->db->delete('user', array('id' => $id));

        // delete user_profile table
        $this->db->delete('user_profile', array('user_id' => $id));
    }

    /**
     * ----------------------------------------------------------------------
     * HELPERS
     * ----------------------------------------------------------------------
     */
	public function has($options = array())
	{
        // WHERE
        // By email
        if (isset($options['email']))
            $this->db->where('email', $options['email']);
		
		// By username
        if (isset($options['username']))
            $this->db->where('username', $options['username']);


        // By except id
        if (isset($options['exceptId']))
            $this->db->where('id <>', $options['exceptId']);

        return $this->db->count_all_results('user');
    }

}

/* End of file User_model.php */
